package com.modules.controller.sys;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.constant.CommonConstant;
import com.common.controller.BaseController;
import com.common.log.LogData;
import com.common.vo.LoginUser;
import com.common.vo.Result;
import com.modules.dto.sys.req.UserEntityReq;
import com.modules.entity.sys.UserEntity;
import com.modules.service.sys.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 用户
 * @author yyq
 *
 */
@Api(value="/sys/user", tags = "用户管理")
@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseController<UserEntity>{

	@Autowired
	private UserService service;
	
	
	/**
	 * 分页查询
	 * @return
	 */
	@GetMapping(value = "/select")
	@ApiOperation(value = "查询接口", notes = "查询接口" , httpMethod = "GET")
	public Result<?> select(UserEntityReq entity,
							@RequestParam(value = "page", defaultValue = "1") Integer page,
							@RequestParam(value = "limit", defaultValue = "10") Integer limit){
		// 根据组织/部门级别查询权限，后期自加
		LoginUser loginUser = getUser();
		entity.setOid(ObjectUtil.isNull(entity.getOid()) ? loginUser.getLastOid() : entity.getOid());
		entity.setPage(getPage(page, limit));
		entity.setLimit(getLimit(page, limit));
		entity.setRoleType(loginUser.getRoleType());
		entity.setRoleTypeValue(CommonConstant.ONE);
		entity.setLoginId(loginUser.getId());
		return service.select(entity);
	}
	
	/**
	 * 查询单个
	 * @return
	 */
	@GetMapping("/selectById")
	@ApiOperation(value = "查询单个接口", notes = "查询单个接口" , httpMethod = "GET")
	public Result<?> selectById(UserEntity entity){
		UserEntity by = service.getById(entity.getId());
		return Result.success(by);
	}
	
	/**
	 * 新增
	 * @return
	 */
	@LogData(module = "用户管理", remarks = "新增")
	@PostMapping("/add")
	@ApiOperation(value = "新增接口", notes = "新增接口" , httpMethod = "POST")
	public Result<?> add(UserEntity entity) {
		entity.setLastOid(entity.getOid());
		return service.insert(entity);
	}
	
	/**
	 * 修改
	 * @return
	 */
	@LogData(module = "用户管理", remarks = "修改")
	@PostMapping("/edit")
	@ApiOperation(value = "修改接口", notes = "修改接口" , httpMethod = "POST")
	public Result<?> edit(UserEntity entity){
		return service.update(entity);
	}
	
	/**
	 * 删除
	 * @return
	 */
	@LogData(module = "用户管理", remarks = "单个删除")
	@DeleteMapping("/delete")
	@ApiOperation(value = "单个删除接口", notes = "单个删除接口" , httpMethod = "POST")
	public Result<?> delete(Integer id){
		if(getUser().getId().equals(id)){
			return Result.fail("权限不足，无法删除");
		}
		service.removeById(id);
		return Result.success(Result.DELETE_MSG);
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@LogData(module = "用户管理", remarks = "批量删除")
	@PostMapping("/deleteBatch")
	@ApiOperation(value = "批量删除接口", notes = "批量删除接口" , httpMethod = "POST")
	public Result<?> deleteBatch(String ids){
		List<UserEntity> list = service.list(new LambdaQueryWrapper<UserEntity>().in(UserEntity::getId, Arrays.asList(ids.split(","))));
		ids = filterIds(list);
		service.removeByIds(Arrays.asList(ids.split(StrUtil.COMMA)));
		return Result.success(Result.DELETE_MSG);
	}

	/**
	 * 过滤非当前权限ID的创建记录
	 * @param list
	 * @return
	 */
	public String filterIds(List<UserEntity> list){
		String ids = StrUtil.EMPTY;
		if(CollUtil.isNotEmpty(list)){
			LoginUser loginUser = getUser();
			for(UserEntity vo : list){
				if(!loginUser.getId().equals(vo.getId())){
					ids += (ids.equals(StrUtil.EMPTY) ? vo.getId() : StrUtil.COMMA+vo.getId());// 实体类自增id
				}
			}
		}
		return ids;
	}
	
	
	
	

}
